package ru.btabank.dom.xpath.expression;

import java.util.ArrayList;

import ru.btabank.dom.xpath.XPathException;

public class CountFunction extends FunctionCall {

    public CountFunction() {
        super();
    }

    public Object evaluate(NodeSet context) throws XPathException {
        ArrayList values = super.evaluateValues(context);
        Object value;

        if (values.size() != 1)
            throw new XPathException("count() function requires one argument.");
        else
            value = values.get(0);

        if (!(value instanceof NodeSet)) {
            throw new XPathException("count() function can only be used for node-sets");
        }

        NodeSet nodeSet = (NodeSet)value;
        return new Double(nodeSet.getLength());
    }
}
